package elephant.servers.gameserver.core;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import elephant.Container;
import elephant.servers.gameserver.GameServer;
import elephant.servers.gameserver.service.GameService;
import elephant.servers.gameserver.service.GameServiceManager;
import elephant.servers.gameserver.service.MethodInfo;
import elephant.servers.gameserver.service.ServiceContextImpl;

/**
 * 
 * @author icecooly
 *
 */
public class GameRequestDispatch {
	//
	private static Logger logger=LoggerFactory.getLogger(GameRequestDispatch.class);
	//
	private GameServiceManager serviceManager;
	private GameServer gameServer;
	
	public GameRequestDispatch(GameServer gameServer,GameServiceManager serviceManager){
		this.gameServer=gameServer;
		this.serviceManager=serviceManager;
	}
	
	public void dispatch(final GameRequest gameRequest){
		//检查serviceId
		GameService service=null;
		MethodInfo methodInfo=null;
		String serviceId=gameRequest.serviceId;
		if(serviceId!=null&&serviceId.indexOf('.')!=-1){
			service=serviceManager.getService(serviceId.substring(0,serviceId.indexOf('.')));
			methodInfo=serviceManager.getMethod(serviceId);
		}
		if(service==null||methodInfo==null){
			logger.warn("can not found service "+serviceId+",gameRequest:"+gameRequest);
			return;
		}
		//
		ServiceContextImpl context=new ServiceContextImpl(gameServer);
		context.setGameRequest(gameRequest);
		//
		GameServerThreadWorker worker=new GameServerThreadWorker(context, service,methodInfo);
		Container.get().executeThreadWorker(worker);
	}
	//	
}
